﻿package com.csd.excelimporter;

import java.util.*;

import com.csd.dao.TeacherDAO;
import com.csd.excelreader.CellConfigInfo;
import com.csd.excelreader.ExcelFormatChecker;
import com.csd.excelreader.StringCellConfig;
import com.csd.pojo.Teacher;


public class TeacherImporter extends BaseImporter {
	
	public TeacherImporter() throws Exception
	{
		super();
	}

	@Override
	protected boolean initChecker() {
		Vector<CellConfigInfo> restriction = new Vector<CellConfigInfo>();
		
		/*
		 * 教师登录名，可用教师编号
		 * 第0列，字符串，1-32字符，不为空
		 */
		StringCellConfig clum = new StringCellConfig ();			
		clum.setClumIndex ( 0 );
		clum.setLowerLimit ( 1 );
		clum.setUpperLimit ( 20 );
		clum.setEmptyEnable ( false );		
		if ( !restriction.add ( clum ) )
			return false;
		
		/*
		 * 教师密码
		 * 第1列，字符串，1-20，不为空
		 */
		clum = new StringCellConfig ();
		clum.setClumIndex ( 1 );
		clum.setLowerLimit ( 1 );
		clum.setUpperLimit ( 20 );
		clum.setEmptyEnable ( false );		
		if ( !restriction.add ( clum ) )
			return false;
		
		/*
		 * 教师真实姓名
		 * 第2列，字符串，1-32，不为空
		 */
		clum = new StringCellConfig ();
		clum.setClumIndex ( 2 );
		clum.setLowerLimit ( 1 );
		clum.setUpperLimit ( 20 );
		clum.setEmptyEnable ( false );		
		if ( !restriction.add ( clum ) )
			return false;

		
		if ( this.checker == null ){
			this.checker = new ExcelFormatChecker();
		}

		this.checker.setRestriction( restriction );
		
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void save() throws ExcelImportException {
		if ( this.excelVal == null )
		{
			this.databaseVal = null;
			throw new ExcelImportException("not pass data checking");
		}
		
		TeacherDAO w = (TeacherDAO)this.writer;
		
		Teacher teacher= (Teacher)this.excelVal;
		
		try
		{
			System.out.println("name: " + teacher.getName() + "\tusername:  "
					+ teacher.getUsername());
			w.save(teacher);
		}
		catch ( RuntimeException re )
		{
			this.excelVal = null;
			this.databaseVal = null;
			throw new ExcelImportException( "save Err !", re);
		}	
		this.excelVal = null;
		this.databaseVal = null;
		
	}

	@Override
	protected boolean checkDBData() {
		if ( this.excelVal == null )
		{
			return false;
		}
		

		return true;
	}

	@Override
	protected boolean clearTable() {
		TeacherDAO dao = (TeacherDAO)this.writer;
		List<Teacher> lst = dao.findAll();
		for ( Teacher i : lst )
		{
			dao.delete(i);
		}
		return false;
	}

	@Override
	protected void findoutFromDatabase() {
				
		if ( this.excelVal == null )
		{
			return;
		}
		TeacherDAO sdao = (TeacherDAO)this.writer;
		Teacher teacher = (Teacher)this.excelVal;
		
		List<Teacher> lst = sdao.findByUsername( teacher.getUsername() );
		
		if( lst != null && lst.size()>0 )
			this.databaseVal = lst.get(0);
		else
			this.databaseVal = null;
	}

	@Override
	protected void readInBuff() {
		this.excelVal = null;
		
		TeacherDAO w = (TeacherDAO)this.writer;
		
		Teacher teacher= new Teacher();
		
		teacher.setUsername( this.reader.readCellAsString(0) );		
		teacher.setPassword( this.reader.readCellAsString(1) );		
		teacher.setName( this.reader.readCellAsString(2) );				

		this.excelVal = teacher;
	}

	@Override
	protected void update() throws ExcelImportException {
		if ( this.excelVal == null )
		{
			this.databaseVal = null;
			throw new ExcelImportException("not pass data checking");
		}
		
		TeacherDAO w = (TeacherDAO)this.writer;	
		Teacher dbteacher = (Teacher)this.databaseVal;
		Teacher teacher= (Teacher)this.excelVal;
		
		dbteacher.setName(teacher.getName());
		dbteacher.setPassword(teacher.getPassword());
		
		try
		{
			System.out.println("name: " + teacher.getName() + "\tusername:  "
					+ teacher.getUsername());
			w.updateTeacher(dbteacher);
		}
		catch ( RuntimeException re )
		{
			this.excelVal = null;
			this.databaseVal = null;
			throw new ExcelImportException( "save Err !", re);
		}	
		this.excelVal = null;
		this.databaseVal = null;
	}

}
